#pragma once
#include "../ConnectionLibrary.h"
#include "ClientConnectionLibraryImpl.h"
#include "../../Messages/BaseMessages.h"
#include "SocketReaderWriter.h"

#include <boost/shared_ptr.hpp>
#include <boost/asio.hpp>
#include <boost/thread/condition.hpp>
#include <boost/thread/mutex.hpp>
#include <boost/thread/locks.hpp>

using boost::asio::ip::tcp;

namespace AlgoTrading
{
	namespace ConnectionLibrary
	{

		class FeedConnectorImpl : public FeedConnector, public SocketNotificationHandler
		{
		public:

			FeedConnectorImpl(boost::asio::io_service& io_service, std::tr1::weak_ptr<NotificationHandler> notificationHandler);
			virtual ~FeedConnectorImpl();

			boost::system::error_code Connect(const std::string& host, const std::string& serviceNameOrPort);
			void Disconnect();

			void SendRequest(const AlgoTrading::Messages::RequestWithoutResponse& request);
			void SendRequest(const AlgoTrading::Messages::RequestWithResponse& request);

			virtual void ParseMessageData(std::istream& stream);
			virtual void OnError(const boost::system::error_code& error);
			virtual void OnDisconnect();

		private:

			//weak_ptr to notificate client
			std::tr1::weak_ptr<NotificationHandler> notificationHandler;

			boost::asio::io_service& io_service;
			
			//transport handle
			std::auto_ptr<tcp::socket> socket;
			boost::shared_ptr<SocketReaderWriter> socketReaderWriter;
		};
	}
}